package com.example.backend.controller;

import com.example.backend.model.GoodsType;
import com.example.backend.service.GoodsTypeService;
import com.example.backend.utils.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/goodstype")
public class GoodsTypeController {
    @Autowired
    GoodsTypeService service;

    @GetMapping
    public Response<List<GoodsType>> findAll() {
        List<GoodsType> allTypes = service.findAll();
        return Response.Ok(
                "all types",
                allTypes
        );
    }

    public Response<GoodsType> findOne(@RequestParam("id") Integer id) {
        GoodsType type = service.findOne(id);
        return Response.Ok(
                "type",
                type
        );
    }

    @PostMapping
    public Response<Integer> insertOne(@RequestBody GoodsType goodsType) {
        int result = service.insertOne(goodsType);
        return result != -1 ?
                Response.Ok("insert success", result)
                : Response.Error("insert failed", result);
    }

    public Response<Integer> updateOne(@RequestBody GoodsType goodsType) {
        int result = service.updateOne(goodsType);
        return result != -1 ?
                Response.Ok("update success", result)
                : Response.Error("insert failed", result);
    }

    public Response<Integer> deleteOne(@RequestParam("id") Integer id) {
        int result = service.deleteOne(id);
        return result != -1 ?
                Response.Ok("delete failed", result)
                : Response.Error("insert failed", result);
    }


}
